#include "stdafx.h"
#include "VertialCloudRenderable.h"
#include "PerspectiveCamera.h"
#include "MFCShowPoseView.h"


CVertialCloudRenderable::CVertialCloudRenderable()
{
	m_topVertices = NULL;
}
CVertialCloudRenderable::CVertialCloudRenderable(NODE * _current,double _x,double _y,double _delta)
{
	currentNode = _current;
	x = _x;
	y = _y;
	delta = _delta;
	m_topVertices = NULL;
}

CVertialCloudRenderable::~CVertialCloudRenderable()
{
}

bool CVertialCloudRenderable::Initialize(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	if (!isOn )
	{
		return false;
	}
	if (isOn && !isInitialize)
	{	
		if (currentNode!=NULL)
		{
			m_topVertices = new PositionColored[1080];

			for (int i = 0; i < 1080; i++)
			{
				double d = currentNode->data.d[i];
				if (d>=15000)
				{
					d = 0;
				}
				double angle = -PI / 4 + i*PI * 3 / 2 / 1080;
				if (angle < 0)
				{
					angle += PI * 2;
				}
				delta=std::fmod(delta ,PI*2);
				if (delta<0)
				{
					delta += 2 * PI ;
				}

				m_topVertices[i].Position.x = x + (d*std::cos(angle)*std::sin(delta)) * TS_MAP_SCALE;
				m_topVertices[i].Position.y = y + (d*std::cos(angle)*std::cos(delta) * TS_MAP_SCALE);
				/*m_topVertices[i].Position.x = x + (d*std::cos(angle)*std::cos(delta) * TS_MAP_SCALE);
				m_topVertices[i].Position.y = y + (d*std::cos(angle)*std::sin(delta)) * TS_MAP_SCALE;*/
				m_topVertices[i].Position.z = d*std::sin(angle) * TS_MAP_SCALE ;
				m_topVertices[i].colour = D3DCOLOR_XRGB(0, 255, 0);
			}
		}
		isInitialize = true;
	}
}

void CVertialCloudRenderable::Update(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	if (isOn && !isInitialize)
	{
		Initialize(g_pd3dDevice);
	}
}

void CVertialCloudRenderable::Render(LPDIRECT3DDEVICE9  g_pd3dDevice)
{
	if (isOn && isInitialize)
	{
		if (m_topVertices!=NULL)
		{
			g_pd3dDevice->SetFVF(D3DFVF_POSITIONCOLORED);
			g_pd3dDevice->DrawPrimitiveUP(D3DPT_POINTLIST, 1079, m_topVertices, sizeof(PositionColored));
		}
		
	}
}
